package com.wr.client.handler;

import com.wr.service.com.wr.request.RpcRequest;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.EventExecutorGroup;

import java.util.concurrent.Callable;

/**
 * @author 王瑞
 * @description: TODO
 * @date 2020/3/21  20:30
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext context;

    private String result;

    private Object para;


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
       context = ctx;
    }

//    收到服务端响应的数据  唤醒线程
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        result = msg.toString();
        notify();
    }

    /*写入数据 等待被唤醒*/
    public synchronized Object call() throws Exception {
        System.out.println("发送的数据是："+para.toString());
        context.writeAndFlush(para);
        wait();
        return result;
    }

    /*设置参数*/
    public void setPara(Object para) {
        this.para = para;
    }
}
